package com.anjie.powerproject.service.impl;

import com.anjie.powerproject.entity.Device;
import com.anjie.powerproject.entity.GraphDev;
import com.anjie.powerproject.pojo.CustomAtlasDevice;
import com.anjie.powerproject.repository.GraphDevRepository;
import com.anjie.powerproject.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class GraphDevServiceImpl {

    @Autowired
    private GraphDevRepository graphDevRepository;

    @Autowired
    private DeviceService deviceService;

    public List<GraphDev> findGraphInfosByGraphId(Integer graphId){
        return graphDevRepository.findGraphDevsByGraphId(graphId);
    }

    public List<Device> getDevicesByGraphId(List<GraphDev> graphDevs){
        List<Device> devices = new ArrayList<>();
        if(graphDevs != null){
            for (GraphDev g:graphDevs){
                Device device = deviceService.findDeviceById(g.getDeviceId());
                devices.add(device);
            }
        }
        return devices;
    }

    public List<CustomAtlasDevice> selectAtlasDeviceByGraphId(Integer graphId){
        return graphDevRepository.selectAtlasDeviceByGraphId(graphId);
    }

    public List selectCustomDevicesByGraphId(Integer graphId){
        return graphDevRepository.selectCustomDevicesByGraphId(graphId);
    }

    public void deleteGraphDevsByGraphId(Integer graphId){
        graphDevRepository.deleteGraphDevsByGraphId(graphId);
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public void updateDeviceCoordinates(List<GraphDev> graphDevs){
        for (GraphDev g:graphDevs){
            //System.out.println(g.getxCoordinates()+"---"+g.getyCoordinates()+"---"+g.getDeviceId()+"---"+g.getGraphId());
            int res = graphDevRepository.updateDeviceCoordinates(g.getxCoordinates(),g.getyCoordinates(),g.getDeviceId(),g.getGraphId(),g.getVersion());
            if (res == 0) {
                throw new RuntimeException("不允许并发修改");
            }
        }
    }

    public void saveDeviceCoordinates(List<GraphDev> graphDevs){
        for (GraphDev g:graphDevs){
            //System.out.println(g.getxCoordinates()+"---"+g.getyCoordinates()+"---"+g.getDeviceId()+"---"+g.getGraphId());
            graphDevRepository.saveAndFlush(g);
        }
    }

    public List<com.anjie.powerproject.strategy.pojo.device.BaseDevice> findSummerDevicesByGraphId(Integer graphId, Integer type){
        return graphDevRepository.selectDeviceByGraphIdInSummer(graphId,type);
    }

    public GraphDev findGraphDevsByDeviceIdAndGraphId(GraphDev graphDev){
        List<GraphDev> graphDevList = graphDevRepository.findGraphDevsByDeviceIdAndGraphId(graphDev.getDeviceId(),graphDev.getGraphId());
        return graphDevList==null||graphDevList.size()==0?null:graphDevList.get(0);
    }

    public boolean findGraphDevsByDeviceId(Integer deviceId){
        List<GraphDev> graphDevs = graphDevRepository.findGraphDevsByDeviceId(deviceId);
        return graphDevs==null||graphDevs.size()==0?false:true;
    }
}
